<template>
  <view class="zxc_centent">
    <view class="cen">
      <image src="/static/images/logo.png" class="logo_img"></image>
      <!-- <view class="name">上门按摩</view> -->
      <view class="btn" @click="login">
        <image
          src="../../static/images/wx.png"
          class="wx_img"
          mode="widthFix"
        ></image>
        微信一键登录
      </view>
    </view>
    <view class="gbc">
      <radio
        value="r1"
        checked="true"
        style="transform: scale(0.7)"
        color="#07c160"
      />
      登录即同意<navigator
        hover-class="none"
        url="/pagesA/aritcle/text?type=1"
        class="ird"
        >《用户协议》</navigator
      >和
      <navigator
        hover-class="none"
        url="/pagesA/aritcle/text?type=2"
        class="ird"
        >《隐私政策》</navigator
      >
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      requesturl: "https://server.xinguanjia.net",
    };
  },
  onLoad(e) {
    let code = this.getUrlCode("code") || "";
    if (code !== null && code !== "") {
      uni.setStorageSync("code", code);
      this.getToken(code);
    }
  },
  methods: {
    // 登录获取获取code地址
    login() {
      // this.getToken()
      let code = this.getUrlCode("code") || "";
      if (!code) {
        uni.request({
          url: this.requesturl + "/customer/login/getOauthUrl",
          method: "GET",
          data: {
            redirectUri: "https://customer.xinguanjia.net/#/pages/login/index",
            // redirectUri: 'http://192.168.1.6:8081/#/pages/login/index'
          },
          header: {},
          success: (res) => {
            console.log(res, "res");
            if (res.data.code === 200) {
              window.location.href = res.data.data;
            }
          },
          fail: (res) => {
            uni.showToast({
              title: res.data.msg,
              icon: "none",
            });
          },
        });
      }
    },
    // 获取路径当中的code
    getUrlCode(name) {
      return (
        decodeURIComponent(
          (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
            location.href
          ) || [, ""])[1].replace(/\+/g, "%20")
        ) || null
      );
    },
    // 登录获取token
    getToken(code) {
      // getToken() {
      this.$post(
        this.$api.customerLogin,
        {
          code,
        },
        (res) => {
          let {
            customerId,
            token,
            bossId,
            storeId,
            headimgurl,
            customerName,
            gender,
            nonPhone,
          } = res.data;
          if (res.code === 200) {
            // uni.setStorageSync("token", 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjgxMmI2Mjk2LTBkZmMtNGRiNi1iNzkwLTcxNmFmODVjYjM0ZCJ9.APke-IYKwUtuiXpTzMCflRkBuZzvos9j_3-ZfFK_Nye9urAqy6zzu6Q3UNZ91jTbOJyIn-la82T49jIWpfydFA');
            // uni.setStorageSync("customerId", '1729443227598610432');
            // uni.setStorageSync("bossId", '1698900935532105728');
            // uni.setStorageSync("storeId", '1698904275582210048');
            // uni.setStorageSync("headimgurl", 'https://thirdwx.qlogo.cn/mmopen/vi_32/8Pp9cJvMGMnR2p8286stk8z4ciaelvmMFhUvpBXxKl3TIgibsfq8icDrb3hAVy8kXg4hcZpRvhLy5yAshibuQicAZnw/132');
            // uni.setStorageSync("customerName", '水萧子木');

            uni.setStorageSync("token", token);
            uni.setStorageSync("customerId", customerId);
            uni.setStorageSync("bossId", bossId);
            uni.setStorageSync("storeId", storeId);
            uni.setStorageSync("headimgurl", headimgurl);
            uni.setStorageSync("customerName", customerName);
            uni.setStorageSync("nonPhone", nonPhone);
            uni.switchTab({
              url: "/pages/item/index",
            });
          }
        },
        (res) => {
          uni.showToast({
            title: res.msg ? res.msg : "网络错误",
            icon: "none",
          });
        },
        1,
        0
      );
    },
  },
};
</script>

<style>
.zxc_centent {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  padding: 0;
  background-color: #fff;
}

.cen {
  width: 80%;
  text-align: center;
}

.name {
  color: #07c160;
  font-size: 35upx;
  font-weight: 700;
  margin: 30upx 0 100upx;
}

.btn {
  width: 100%;
  margin: 250upx 0 0;
  padding: 25upx 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #07c160;
  color: #fff;
  border-radius: 100upx;
}

.register {
  margin-top: 30upx;
  color: #07c160;
  text-align: right;
  font-size: 26upx;
}

.wx_img {
  width: 40upx;
  margin-right: 20upx;
}

.logo_img {
  width: 160upx;
  height: 160upx;
  border-radius: 160upx;
}

.gbc {
  width: 90%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  font-size: 26upx;
  color: #999;
  justify-content: center;
}

.ird {
  color: #07c160;
}

.fcvd {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 90%;
  margin: 30upx auto;
}

.ifk {
  color: #07c160;
  padding: 20upx 0;
  font-weight: 700;
  font-size: 28upx;
}
</style>